<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

require_once './assets/lib/phpword/PHPWord.php';
class Document_Parser {
    //put your code here
    function __construct() {
    }
    function extractDocxToXML($filename) {
        //Check for extension
        $ext = end(explode('.', $filename));
        if($ext == 'docx'){
            //if its docx file
            $dataFile = "word/document.xml";}
        else{
            //else it must be odt file
            $dataFile = "content.xml";
        }     
        //Create a new ZIP archive object
        $zip = new ZipArchive;
        // Open the archive file
        $bool = $zip->open("C://xampp/htdocs/lis/assets/documents/template/docx/billoflading.docx");
        echo "---";
        echo $bool;
        echo "---";
        if (true === $bool ) {
            echo "success";
            // If successful, search for the data file in the archive
            if (($index = $zip->locateName($dataFile)) !== false) {
                // Index found! Now read it to a string
                $text = $zip->getFromIndex($index);
                // Load XML from a string
                // Ignore errors and warnings
                $xml = DOMDocument::loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
                // Remove XML formatting tags and return the text
                return strip_tags($xml->saveXML());
            }
            //Close the archive file
            $zip->close();
        }else{
            echo "failed";
        }

        // In case of failure return a message
        return "File not found";
    }
    
    /*** GENERATOR XML TO ARRAY ***/
    function convert_blxml_to_array($locationfile){
        // input : xml Bill Of Lading
        // output : billoflading_entity
        $xml = simplexml_load_file($locationfile);
	$result = array();
	$data = array();
	// ambil header
	$ShippingLinesHeader = $xml->xpath("ShippingLinesHeader");
	foreach($ShippingLinesHeader as $rows){
		$data['SLName'] = (string) $rows->SLName;
		$data['SLAddress'] = (string) $rows->SLAddress;
		$data['SLTelp'] = (string) $rows->SLTelp;
	}
	
	// ambil body
	$body = $xml->xpath("Body");
	foreach($body as $rowbody){
		$document = $rowbody->Document;
		$party = $rowbody->Party;
		$lineitems = $rowbody->LineItem;
		$freight = $rowbody->Freight;
	}
	
	// extract document
	foreach($document as $rows){
		$data['CreationDate'] = (string) $rows->CreationDate;
		$data['BLNumber'] = (string) $rows->BLNumber;
	}
	// extract party
	foreach($party as $outrows){
		// ambil shipper
		$shipper = $outrows->Shipper;
		foreach($shipper as $inrows){
			$data['SHName'] = (string) $inrows->SHName;
			$data['SHAddress'] = (string) $inrows->SHAddress;
			$data['SHTelp'] = (string)$inrows->SHTelp;
		}
		// ambil consignee
		$consignee = $outrows->Consignee;
		foreach($consignee as $inrows){
			$data['CName'] = (string)$inrows->CName;
			$data['CAddress'] = (string)$inrows->CAddress;
			$data['CTelp'] = (string)$inrows->CTelp;
		}
		// ambil notifyparty
		$notifyparty = $outrows->NotifyParty;
		foreach($notifyparty as $inrows){
			$data['NPName'] = (string)$inrows->NPName;
			$data['NPAddress'] = (string)$inrows->NPAddress;
			$data['NPTelp'] = (string)$inrows->NPTelp;
		}
		// ambil forwarder
		$forwarder = $outrows->Forwarder;
		foreach($forwarder as $inrows){
			$data['FName'] = (string)$inrows->FName;
			$data['FAddress'] = (string)$inrows->FAddress;
			$data['FTelp'] = (string)$inrows->FTelp;
		}
		// ambil precarriage, placeofreceipt, vessel, portofloading, port of discharge, place of delivery
		$data['Precarriage'] = (string)$outrows->Precarriage;
		$data['PlaceOfReceipt'] = (string)$outrows->PlaceOfReceipt;
		$data['Vessel'] = (string)$outrows->Vessel;
		$data['PortOfLoading'] = (string)$outrows->PortOfLoading;
		$data['PortOfDischarge'] = (string)$outrows->PortOfDischarge;
		$data['PlaceOfDelivery'] = (string)$outrows->PlaceOfDelivery;
	}
	
	// extract LineItem
	$arraycontainerdetail = array();
	foreach($lineitems as $lineitem){
		$containers = $lineitem->Container;
		foreach($containers as $container){
			$container_array = array();
			$container_array['ContainerNo'] = (string) $container->ContainerNo;
			$container_array['KindPackage'] = (string) $container->KindPackage;
			$container_array['GrossWeight'] = (string) $container->GrossWeight;
			$container_array['NettWeight'] = (string) $container->NettWeight;
			$container_array['Measurement'] = (string)$container->Measurement;
			$containerdetails = $container->ContainerDetail;
			foreach($containerdetails as $containerdetail){
				$container_detail_array = array();
				$container_array['ContainerDetail'] = array();
				$details = $containerdetail->Detail;
				foreach($details as $detail){
					$container_detail_array['QuantityPackage'] = (string) $detail->QuantityPackage;
					$container_detail_array['Description'] = (string) $detail->Description;
					array_push($container_array['ContainerDetail'], $container_detail_array);
				}
			}
			$container_array['TotalPackage'] = (string) $container->TotalPackage;
			array_push($arraycontainerdetail, $container_array);
		}
	}
	
	// extract freight
	foreach($freight as $outrows){
		$deliveryagent = $outrows->DeliveryAgent;
		foreach($deliveryagent as $inrows){
			$data['DAName'] = (string)$inrows->DAName;
			$data['DAAddress'] = (string)$inrows->DAAddress;
			$data['DATelp'] = (string)$inrows->DATelp;
		}
		$data['FreightCharges'] = (real)$outrows->FreightCharges;
		$data['RevenueTons'] = (real)$outrows->RevenueTons;
		$data['Prepaid'] = (real)$outrows->Prepaid;
		$data['Collect'] = (real)$outrows->Collect;
		$data['ExchangeRate'] = (real)$outrows->ExchangeRate;
		$data['PrepaidAt'] = (string)$outrows->PrepaidAt;
		$data['PayableAt'] = (string)$outrows->PayableAt;
		$data['PlaceOfIssue'] = (string)$outrows->PlaceOfIssue;
		$data['DateOfIssue'] = (string)$outrows->DateOfIssue;
		$data['NoOfOriginalBill'] = (string)$outrows->NoOfOriginalBill;
	}
	// print_r($data);
	$result['data'] = $data;
	$result['lineitem'] = $arraycontainerdetail;
	return $result;
    }
    
    function convert_fixml_to_array($locationfile){
        // input : xml Freight of Invoice
        $xml = simplexml_load_file($locationfile);
	$result = array();
	$data = array();
	// ambil header
	$ShippingLinesHeader = $xml->xpath("ShippingLinesHeader");
	foreach($ShippingLinesHeader as $rows){
		$data['SLName'] = (string) $rows->SLName;
		$data['SLAddress'] = (string) $rows->SLAddress;
		$data['SLTelp'] = (string) $rows->SLTelp;
	}
	
	// ambil body
	$body = $xml->xpath("Body");
	foreach($body as $rowbody){
		$document = $rowbody->Document;
		$party = $rowbody->Party;
		$lineitems = $rowbody->LineItem;
		$freightsummary = $rowbody->FreightSummary;
	}
	
	// extract document
	foreach($document as $rows){
		$data['CreationDate'] = (string) $rows->CreationDate;
		$data['FINumber'] = (string) $rows->FINumber;
	}
	// extract party
	foreach($party as $outrows){
		// ambil shipper
		$shipper = $outrows->Shipper;
		foreach($shipper as $inrows){
			$data['SHName'] = (string) $inrows->SHName;
			$data['SHAddress'] = (string) $inrows->SHAddress;
			$data['SHTelp'] = (string)$inrows->SHTelp;
		}
		// ambil consignee
		$consignee = $outrows->Consignee;
		foreach($consignee as $inrows){
			$data['CName'] = (string)$inrows->CName;
			$data['CAddress'] = (string)$inrows->CAddress;
			$data['CTelp'] = (string)$inrows->CTelp;
		}
		// ambil notifyparty
		$notifyparty = $outrows->NotifyParty;
		foreach($notifyparty as $inrows){
			$data['NPName'] = (string)$inrows->NPName;
			$data['NPAddress'] = (string)$inrows->NPAddress;
			$data['NPTelp'] = (string)$inrows->NPTelp;
		}
		// ambil forwarder
		$forwarder = $outrows->Forwarder;
		foreach($forwarder as $inrows){
			$data['FName'] = (string)$inrows->FName;
			$data['FAddress'] = (string)$inrows->FAddress;
			$data['FTelp'] = (string)$inrows->FTelp;
		}
		// ambil precarriage, placeofreceipt, vessel, portofloading, port of discharge, place of delivery
		$data['Precarriage'] = (string)$outrows->Precarriage;
		$data['PlaceOfReceipt'] = (string)$outrows->PlaceOfReceipt;
		$data['Vessel'] = (string)$outrows->Vessel;
		$data['PortOfLoading'] = (string)$outrows->PortOfLoading;
		$data['PortOfDischarge'] = (string)$outrows->PortOfDischarge;
		$data['PlaceOfDelivery'] = (string)$outrows->PlaceOfDelivery;
	}
	
	// extract LineItem
	$arraycontainerdetail = array();
	foreach($lineitems as $lineitem){
		$containers = $lineitem->Container;
		foreach($containers as $container){
			$container_array = array();
			$container_array['ContainerNo'] = (string) $container->ContainerNo;
			$container_array['KindPackage'] = (string) $container->KindPackage;
			$container_array['GrossWeight'] = (string) $container->GrossWeight;
			$container_array['NettWeight'] = (string) $container->NettWeight;
			$container_array['Measurement'] = (string)$container->Measurement;
			$containerdetails = $container->ContainerDetail;
			foreach($containerdetails as $containerdetail){
				$container_detail_array = array();
				$container_array['ContainerDetail'] = array();
				$details = $containerdetail->Detail;
				foreach($details as $detail){
					$container_detail_array['QuantityPackage'] = (string) $detail->QuantityPackage;
					$container_detail_array['Description'] = (string) $detail->Description;
					$container_detail_array['Rate'] = (string) $detail->Rate;
					$container_detail_array['Charges'] = (string) $detail->Charges;
					array_push($container_array['ContainerDetail'], $container_detail_array);
				}
			}
			$container_array['TotalPackage'] = (string) $container->TotalPackage;
			array_push($arraycontainerdetail, $container_array);
		}
	}
	
	// extract freightsummary
	foreach($freightsummary as $outrows){
		$data['TotalFreightPrepaid'] = (string)$outrows->TotalFreightPrepaid;
		$data['TotalFreightCollect'] = (string)$outrows->TotalFreightCollect;
		$data['TotalFreight'] = (string)$outrows->TotalFreight;
	}
	// print_r($data);
	$result['data'] = $data;
	$result['lineitem'] = $arraycontainerdetail;
        return $result;
	
    }

    function convert_doxml_to_array($locationfile){
        $xml = simplexml_load_file($locationfile);
	$result = array();
	$data = array();
	// ambil header
	$ShippingLinesHeader = $xml->xpath("ShippingLinesHeader");
	foreach($ShippingLinesHeader as $rows){
		$data['SLName'] = (string) $rows->SLName;
		$data['SLAddress'] = (string) $rows->SLAddress;
		$data['SLTelp'] = (string) $rows->SLTelp;
	}
	
	// ambil body
	$body = $xml->xpath("Body");
	foreach($body as $rowbody){
		$document = $rowbody->Document;
		$deliverto = $rowbody->DeliverTo;
		$deliverfrom = $rowbody->DeliverFrom;
		$lineitems = $rowbody->LineItem;
	}
	
	// extract document
	foreach($document as $rows){
		$data['CreationDate'] = (string) $rows->CreationDate;
		$data['DONumber'] = (string) $rows->DONumber;
		$data['BLNumber'] = (string) $rows->BLNumber;
		$data['BC11Number'] = (string) $rows->BC11Number;
	}
	
	// extract deliverto
	foreach($deliverto as $rows){
		$deliveredto = $rows->DeliveredTo;
		$issuedto = $rows->IssuedTo;
	}
		// extract deliveredto
		foreach($deliveredto as $rows){
			$data['DTName'] = (string) $rows->DTName;
			$data['DTAddress'] = (string) $rows->DTAddress;
			$data['DTTelp'] = (string) $rows->DTTelp;
		}
		// extract issuedto
		foreach($issuedto as $rows){
			$data['ITName'] = (string) $rows->ITName;
			$data['ITAddress'] = (string) $rows->ITAddress;
			$data['ITTelp'] = (string) $rows->ITTelp;
		}
	// extract deliverfrom
	foreach($deliverfrom as $rows){
		$data['Terminal'] = (string) $rows->Terminal;
		$data['Vessel'] = (string) $rows->Vessel;
		$data['PortOfLoading'] = (string) $rows->PortOfLoading;
		$data['PortOfDischarge'] = (string) $rows->PortOfDischarge;
		$data['GoDown'] = (string) $rows->GoDown;
		$data['ArriveOn'] = (string) $rows->ArriveOn;
	}
	// extract LineItem
	$arraycontainerdetail = array();
	foreach($lineitems as $lineitem){
		$containers = $lineitem->Container;
		foreach($containers as $container){
			$container_array = array();
			$container_array['ContainerNo'] = (string) $container->ContainerNo;
			$container_array['KindPackage'] = (string) $container->KindPackage;
			$container_array['GrossWeight'] = (string) $container->GrossWeight;
			$container_array['NettWeight'] = (string) $container->NettWeight;
			$container_array['Measurement'] = (string)$container->Measurement;
			$containerdetails = $container->ContainerDetail;
			foreach($containerdetails as $containerdetail){
				$container_detail_array = array();
				$container_array['ContainerDetail'] = array();
				$details = $containerdetail->Detail;
				foreach($details as $detail){
					$container_detail_array['QuantityPackage'] = (string) $detail->QuantityPackage;
					$container_detail_array['Description'] = (string) $detail->Description;
					array_push($container_array['ContainerDetail'], $container_detail_array);
				}
			}
			$container_array['TotalPackage'] = (string) $container->TotalPackage;
			array_push($arraycontainerdetail, $container_array);
		}
	}
	$result['data'] = $data;
	$result['lineitem'] = $arraycontainerdetail;
	return $result;
    }
    
    function convert_sppbxml_to_array($locationfile){
        
        $xml = simplexml_load_file($locationfile);
        $result = array();
	$data = array();
	// ambil header
	$ShippingLinesHeader = $xml->xpath("CustomsHeader");
	foreach($ShippingLinesHeader as $rows){
		$data['Ministry'] = (string) $rows->Ministry;
		$data['Customs'] = (string) $rows->Customs;
		$data['Office'] = (string) $rows->Office;
	}
	
	// ambil body
	$body = $xml->xpath("Body");
	foreach($body as $rowbody){
		$document = $rowbody->Document;
		$party = $rowbody->Party;
		$lineitems = $rowbody->LineItem;
	}
	
	// extract document
	foreach($document as $rows){
		$data['CreationDate'] = (string) $rows->CreationDate;
		$data['SPPBNumber'] = (string) $rows->SPPBNumber;
		$data['PIBNumber'] = (string) $rows->PIBNumber;
	}
	
	// extract party
	foreach($party as $outrows){
		// ambil consignee
		$consignee = $outrows->Consignee;
		foreach($consignee as $inrows){
			$data['CNPWP'] = (string)$inrows->CNPWP;
			$data['CName'] = (string)$inrows->CName;
			$data['CAddress'] = (string)$inrows->CAddress;
		}
		// ambil forwarder
		$ppjk = $outrows->PPJK;
		foreach($ppjk as $inrows){
			$data['PPJKNPWP'] = (string)$inrows->PPJKNPWP;
			$data['PPJKName'] = (string)$inrows->PPJKName;
			$data['PPJKAddress'] = (string)$inrows->PPJKAddress;
		}
		// ambil precarriage, placeofreceipt, vessel, portofloading, port of discharge, place of delivery
		$data['GoodsLocation'] = (string)$outrows->GoodsLocation;
		$data['BLNumber'] = (string)$outrows->BLNumber;
		$data['BLDate'] = (string)$outrows->BLDate;
		$data['Vessel'] = (string)$outrows->Vessel;
		$data['Voyage'] = (string)$outrows->Voyage;
		$data['BC11Number'] = (string)$outrows->BC11Number;
		$data['BC11Date'] = (string)$outrows->BC11Date;
		$data['BC11Post'] = (string)$outrows->BC11Post;
		$data['TotalPackage'] = (string)$outrows->TotalPackage;
		$data['WeightPackage'] = (string)$outrows->WeightPackage;
		$data['BrandPackage'] = (string)$outrows->BrandPackage;
		$data['TotalContainer'] = (string)$outrows->TotalContainer;
	}
	
	// extract LineItem
	$arraycontainerdetail = array();
	foreach($lineitems as $lineitem){
		$containers = $lineitem->Container;
		foreach($containers as $container){
			$container_array = array();
			$container_array['ContainerNo'] = (string) $container->ContainerNo;
			$container_array['Size'] = (string) $container->Size;
			$container_array['Penegahan'] = (string) $container->Penegahan;
			$container_array['Explanation'] = (string) $container->Explanation;
			array_push($arraycontainerdetail, $container_array);
		}
	}
	// print_r($data);
	$result['data'] = $data;
	$result['lineitem'] = $arraycontainerdetail;
	//foreach($arraycontdetail as $lineitem){
	//	print_r($lineitem['ContainerDetail']);
	//}
	//print_r($result);
	return $result;
    }
    
    function convert_pibxml_to_array($locationfile){
        $xml = simplexml_load_file($locationfile);
	$result = array();
	$data = array();
	// ambil header
        
	$ShippingLinesHeader = $xml->xpath("ForwarderHeader");
	foreach($ShippingLinesHeader as $rows){
		$data['FName'] = (string) $rows->FName;
		$data['FAddress'] = (string) $rows->FAddress;
		$data['FTelp'] = (string) $rows->FTelp;
	}
	
	// ambil body
	$body = $xml->xpath("Body");
	foreach($body as $rowbody){
		$document = $rowbody->Document;
		$party = $rowbody->Party;
		$lineitems = $rowbody->LineItem;
	}
	
	// extract document
	foreach($document as $rows){
		$data['CustomOffice'] = (string) $rows->CustomOffice;
		$data['PIBNo'] = (string) $rows->PIBNo;
		$data['KindofPIB'] = (string) $rows->KindofPIB;
		$data['KindofImport'] = (string) $rows->KindofImport;
		$data['Payment'] = (string) $rows->Payment;
	}
	
	// extract party
	foreach($party as $outrows){
		// ambil shipper
		$consignor = $outrows->Consignor;
		foreach($consignor as $inrows){
			$data['CoName'] = (string) $inrows->CoName;
			$data['CoAddress'] = (string) $inrows->CoAddress;
			$data['CoTelp'] = (string)$inrows->CoTelp;
		}
		// ambil consignee
		$consignee = $outrows->Consignee;
		foreach($consignee as $inrows){
			$data['CName'] = (string)$inrows->CName;
			$data['CAddress'] = (string)$inrows->CAddress;
			$data['CTelp'] = (string)$inrows->CTelp;
			$data['CStatus'] = (string)$inrows->CStatus;
		}
		// ambil ppjk
		$ppjk = $outrows->PPJK;
		foreach($ppjk as $inrows){
			$data['PPJKNPWP'] = (string)$inrows->PPJKNPWP;
                        $data['PPJKName'] = (string)$inrows->PPJKName;
			$data['PPJKNumber'] = (string)$inrows->PPJKNumber;
			$data['PPJKDatePermission'] = (string)$inrows->PPJKDatePermission;
		}
		// ambil precarriage, placeofreceipt, vessel, portofloading, port of discharge, place of delivery
		$data['Transportation'] = (string)$outrows->Transportation;
		$data['TransportationVoyageNo'] = (string)$outrows->TransportationVoyageNo;
		$data['ArrivalDate'] = (string)$outrows->ArrivalDate;
		$data['LoadingDock'] = (string)$outrows->LoadingDock;
		$data['TransitDock'] = (string)$outrows->TransitDock;
		$data['DischargeDock'] = (string)$outrows->DischargeDock;
		$data['BrandContainer'] = (string)$outrows->BrandContainer;
		$data['GrossWeight'] = (string)$outrows->GrossWeight;
		$data['NettWeight'] = (string)$outrows->NettWeight;
	}
	
	// extract LineItem
	$arraycontainerdetail = array();
	foreach($lineitems as $lineitem){
		$containers = $lineitem->Container;
		foreach($containers as $container){
			$container_array = array();
			$container_array['ContainerNo'] = (string) $container->ContainerNo;
			$container_array['KindPackage'] = (string) $container->KindPackage;
			$container_array['GrossWeight'] = (string) $container->GrossWeight;
			$container_array['NettWeight'] = (string) $container->NettWeight;
			$container_array['Measurement'] = (string)$container->Measurement;
			$containerdetails = $container->ContainerDetail;
			foreach($containerdetails as $containerdetail){
				$container_detail_array = array();
				$container_array['ContainerDetail'] = array();
				$details = $containerdetail->Detail;
				foreach($details as $detail){
					$container_detail_array['QuantityPackage'] = (string) $detail->QuantityPackage;
					$container_detail_array['Description'] = (string) $detail->Description;
					array_push($container_array['ContainerDetail'], $container_detail_array);
				}
			}
			$container_array['TotalPackage'] = (string) $container->TotalPackage;
			array_push($arraycontainerdetail, $container_array);
		}
	}
	// print_r($data);
	$result['data'] = $data;
	$result['lineitem'] = $arraycontainerdetail;
	//foreach($arraycontdetail as $lineitem){
	//	print_r($lineitem['ContainerDetail']);
	//}
	print_r($result);
	return $result;
    }
    
    
    
    /*** GENERATOR ARRAY TO DOCX ***/
    public function convert_blarray_to_docx($docbilloflading, $fileTemplate, $fileDestination){
        /* input : 1. $docbilloflading : array yang memiliki anak isi nya billofladng dan lineitem
         *          2. $template : documen yang dijadikan template untuk billoflading 
         * proses : mengubah array menjadi docx
         * output : true jika berhasil menghasilkan document .docx, false jika gagal
         */
        $PHPWord = new PHPWord();
        //$document = $PHPWord->loadTemplate('./assets/documents/template/docx/bl_output.docx');
        $document = $PHPWord->loadTemplate($fileTemplate);
        if($docbilloflading && $document){
            $billoflading = $docbilloflading['data'];
            $lineitem = $docbilloflading['lineitem'];
            // fill billoflading
            while (list($var, $val) = each($billoflading)) {
                $document->setValue($var,$val);
            }
          
            // fill billoflading line item
            $table = ''; //empty table
            if($lineitem){ //if there was data returned from queryDB()
                $table .= '<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblW w:w="0" w:type="auto"/><w:tblBorders><w:top w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="10" w:space="0" w:color="auto"/></w:tblBorders><w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol w:w="836"/><w:gridCol w:w="975"/><w:gridCol w:w="871"/><w:gridCol w:w="1096"/><w:gridCol w:w="1394"/><w:gridCol w:w="3159"/><w:gridCol w:w="911"/></w:tblGrid><w:tr w:rsidR="006F4097" w:rsidTr="006F4097"><w:tc><w:tcPr><w:tcW w:w="832" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Cont</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> No</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="994" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Kind Package</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="883" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Gross Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1195" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Nett</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1394" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Measurement</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2170" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Detail</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="847" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Total Package</w:t></w:r></w:p></w:tc></w:tr>';
                foreach($lineitem as $debt){
                        $containerdetails = $debt->ContainerDetail;
			$table .= '<w:tr>'; //new xml table row
                        
			$table .= '<w:tc><w:p><w:r><w:t>'; //start cell
			$table .= $debt->ContainerNo; //cell contents
			$table .= '</w:t></w:r></w:p></w:tc>'; //close cell
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->KindPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->GrossWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';		
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->NettWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Measurement;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			foreach($containerdetails as $containerdetail){
                           $table .= $containerdetail->QuantityPackage. ' '.$containerdetail->Description.', ';
                        }
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->TotalPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '</w:tr>';
		} //done with dynamic data
		$table .= '</w:tbl>';
            }
            $document->setValue('LineItem',$table);
            $document->save($fileDestination);
            return true;
        }else{
            return false;    
        }
    }
    
    public function convert_fiarray_to_docx($docfreightinvoice, $fileTemplate, $fileDestination){
        /* input : 1. $docfreightinvoice : array yang memiliki anak isi nya freightinvoice dan lineitem
         *          2. $template : documen yang dijadikan template untuk freightinvoice 
         * proses : mengubah array menjadi docx
         * output : true jika berhasil menghasilkan document .docx, false jika gagal
         */
        $PHPWord = new PHPWord();
        //$document = $PHPWord->loadTemplate('./assets/documents/template/docx/fi_output.docx');
        $document = $PHPWord->loadTemplate($fileTemplate);
        if($docfreightinvoice && $document){
            $freightinvoice = $docfreightinvoice['data'];
            $lineitem = $docfreightinvoice['lineitem'];
            // fill freightinvoice
            while (list($var, $val) = each($freightinvoice)) {
                $document->setValue($var,$val);
            }
          
            // fill freightinvoice line item
            $table = ''; //empty table
            if($lineitem){ //if there was data returned from queryDB()
                $table .= '<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblW w:w="0" w:type="auto"/><w:tblBorders><w:top w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="10" w:space="0" w:color="auto"/></w:tblBorders><w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol w:w="836"/><w:gridCol w:w="975"/><w:gridCol w:w="871"/><w:gridCol w:w="1096"/><w:gridCol w:w="1394"/><w:gridCol w:w="3159"/><w:gridCol w:w="911"/></w:tblGrid><w:tr w:rsidR="006F4097" w:rsidTr="006F4097"><w:tc><w:tcPr><w:tcW w:w="832" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Cont</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> No</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="994" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Kind Package</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="883" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Gross Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1195" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Nett</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1394" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Measurement</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2170" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Detail</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="847" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Total Package</w:t></w:r></w:p></w:tc></w:tr>';
                foreach($lineitem as $debt){
                        $containerdetails = $debt->ContainerDetail;
			$table .= '<w:tr>'; //new xml table row
                        
			$table .= '<w:tc><w:p><w:r><w:t>'; //start cell
			$table .= $debt->ContainerNo; //cell contents
			$table .= '</w:t></w:r></w:p></w:tc>'; //close cell
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->KindPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->GrossWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';		
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->NettWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Measurement;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			foreach($containerdetails as $containerdetail){
                           $table .= $containerdetail->QuantityPackage. ' '.$containerdetail->Description.' ('.$containerdetail->Rate.' @ '.$containerdetail->Charges.'), ';
                        }
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->TotalPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '</w:tr>';
		} //done with dynamic data
		$table .= '</w:tbl>';
            }
            $document->setValue('LineItem',$table);
            $document->save($fileDestination);
            return true;
        }else{
            return false;    
        }
    }
    
    public function convert_doarray_to_docx($docdeliveryorder, $fileTemplate, $fileDestination){
        /* input : 1. $docdeliveryorder : array yang memiliki anak isi nya deliveryorder dan lineitem
         *          2. $template : documen yang dijadikan template untuk deliveryorder
         * proses : mengubah array menjadi docx
         * output : true jika berhasil menghasilkan document .docx, false jika gagal
         */
        $PHPWord = new PHPWord();
        //$document = $PHPWord->loadTemplate('./assets/documents/template/docx/bl_output.docx');
        $document = $PHPWord->loadTemplate($fileTemplate);
        if($docdeliveryorder && $document){
            $deliveryorder = $docdeliveryorder['data'];
            $lineitem = $docdeliveryorder['lineitem'];
            // fill billoflading
            while (list($var, $val) = each($deliveryorder)) {
                $document->setValue($var,$val);
            }
          
            // fill billoflading line item
            $table = ''; //empty table
            if($lineitem){ //if there was data returned from queryDB()
                $table .= '<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblW w:w="0" w:type="auto"/><w:tblBorders><w:top w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="10" w:space="0" w:color="auto"/></w:tblBorders><w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol w:w="836"/><w:gridCol w:w="975"/><w:gridCol w:w="871"/><w:gridCol w:w="1096"/><w:gridCol w:w="1394"/><w:gridCol w:w="3159"/><w:gridCol w:w="911"/></w:tblGrid><w:tr w:rsidR="006F4097" w:rsidTr="006F4097"><w:tc><w:tcPr><w:tcW w:w="832" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Cont</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> No</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="994" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Kind Package</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="883" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Gross Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1195" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Nett</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1394" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Measurement</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2170" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Detail</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="847" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Total Package</w:t></w:r></w:p></w:tc></w:tr>';
                foreach($lineitem as $debt){
                        $containerdetails = $debt->ContainerDetail;
			$table .= '<w:tr>'; //new xml table row
                        
			$table .= '<w:tc><w:p><w:r><w:t>'; //start cell
			$table .= $debt->ContainerNo; //cell contents
			$table .= '</w:t></w:r></w:p></w:tc>'; //close cell
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->KindPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->GrossWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';		
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->NettWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Measurement;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			foreach($containerdetails as $containerdetail){
                           $table .= $containerdetail->QuantityPackage. ' '.$containerdetail->Description.', ';
                        }
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->TotalPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '</w:tr>';
		} //done with dynamic data
		$table .= '</w:tbl>';
            }
            $document->setValue('LineItem',$table);
            $document->save($fileDestination);
            return true;
        }else{
            return false;    
        }
    }

    public function convert_pibarray_to_docx($docpib, $fileTemplate, $fileDestination){
        /* input : 1. $docbilloflading : array yang memiliki anak isi nya billofladng dan lineitem
         *          2. $template : documen yang dijadikan template untuk billoflading 
         * proses : mengubah array menjadi docx
         * output : true jika berhasil menghasilkan document .docx, false jika gagal
         */
        $PHPWord = new PHPWord();
        //$document = $PHPWord->loadTemplate('./assets/documents/template/docx/bl_output.docx');
        $document = $PHPWord->loadTemplate($fileTemplate);
        if($docpib && $document){
            $pib = $docpib['data'];
            $lineitem = $docpib['lineitem'];
            // fill pib
            while (list($var, $val) = each($pib)) {
                //echo $var; echo $val; echo '<br/>';
                $document->setValue($var,$val);
            }
          
            // fill billoflading line item
            $table = ''; //empty table
            if($lineitem){ //if there was data returned from queryDB()
                $table .= '<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblW w:w="0" w:type="auto"/><w:tblBorders><w:top w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:left w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:bottom w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:right w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideH w:val="single" w:sz="10" w:space="0" w:color="auto"/><w:insideV w:val="single" w:sz="10" w:space="0" w:color="auto"/></w:tblBorders><w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol w:w="836"/><w:gridCol w:w="975"/><w:gridCol w:w="871"/><w:gridCol w:w="1096"/><w:gridCol w:w="1394"/><w:gridCol w:w="3159"/><w:gridCol w:w="911"/></w:tblGrid><w:tr w:rsidR="006F4097" w:rsidTr="006F4097"><w:tc><w:tcPr><w:tcW w:w="832" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Cont</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> No</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="994" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Kind Package</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="883" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Gross Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1195" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Nett</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> Weight</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="1394" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Measurement</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2170" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:r w:rsidRPr="0035508B"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Detail</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="847" w:type="dxa"/></w:tcPr><w:p w:rsidR="006F4097" w:rsidRPr="0035508B" w:rsidRDefault="006F4097" w:rsidP="007100CF"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Total Package</w:t></w:r></w:p></w:tc></w:tr>';
                foreach($lineitem as $debt){
                        $containerdetails = $debt->ContainerDetail;
			$table .= '<w:tr>'; //new xml table row
                        
			$table .= '<w:tc><w:p><w:r><w:t>'; //start cell
			$table .= $debt->ContainerNo; //cell contents
			$table .= '</w:t></w:r></w:p></w:tc>'; //close cell
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->KindPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->GrossWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';		
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->NettWeight;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Measurement;
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			foreach($containerdetails as $containerdetail){
                           $table .= $containerdetail->QuantityPackage. ' '.$containerdetail->Description.', ';
                        }
			$table .= '</w:t></w:r></w:p></w:tc>';
                        
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->TotalPackage;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '</w:tr>';
		} //done with dynamic data
		$table .= '</w:tbl>';
            }
            $document->setValue('LineItem',$table);
            $document->save($fileDestination);
            return true;
        }else{
            return false;    
        }
    }
    
    public function convert_sppbarray_to_docx($docsppb, $fileTemplate, $fileDestination){
        /* input : 1. $docbilloflading : array yang memiliki anak isi nya billofladng dan lineitem
         *          2. $template : documen yang dijadikan template untuk billoflading 
         * proses : mengubah array menjadi docx
         * output : true jika berhasil menghasilkan document .docx, false jika gagal
         */
        $PHPWord = new PHPWord();
        //$document = $PHPWord->loadTemplate('./assets/documents/template/docx/bl_output.docx');
        $document = $PHPWord->loadTemplate($fileTemplate);
        if($docsppb && $document){
            $sppb = $docsppb['data'];
            $lineitem = $docsppb['lineitem'];
            // fill pib
            while (list($var, $val) = each($sppb)) {
                //echo $var; echo $val; echo '<br/>';
                $document->setValue($var,$val);
            }
          
            // fill billoflading line item
            $table = ''; //empty table
            if($lineitem){ //if there was data returned from queryDB()
                $table .= '<w:tbl><w:tblPr><w:tblStyle w:val="TableGrid"/><w:tblW w:w="0" w:type="auto"/><w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid><w:gridCol w:w="2394"/><w:gridCol w:w="2394"/><w:gridCol w:w="2394"/><w:gridCol w:w="2394"/></w:tblGrid><w:tr w:rsidR="00F40768" w:rsidTr="00F40768"><w:tc><w:tcPr><w:tcW w:w="2394" w:type="dxa"/></w:tcPr><w:p w:rsidR="00F40768" w:rsidRPr="00F40768" w:rsidRDefault="00F40768" w:rsidP="001C4173"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00F40768"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t>Cont</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="00F40768"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/></w:rPr><w:t xml:space="preserve"> No</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2394" w:type="dxa"/></w:tcPr><w:p w:rsidR="00F40768" w:rsidRPr="00F40768" w:rsidRDefault="00F40768" w:rsidP="001C4173"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/><w:lang w:val="id-ID"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00F40768"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/><w:lang w:val="id-ID"/></w:rPr><w:t>Size</w:t></w:r><w:proofErr w:type="spellEnd"/></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2394" w:type="dxa"/></w:tcPr><w:p w:rsidR="00F40768" w:rsidRPr="00F40768" w:rsidRDefault="00F40768" w:rsidP="001C4173"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/><w:lang w:val="id-ID"/></w:rPr></w:pPr><w:r w:rsidRPr="00F40768"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/><w:lang w:val="id-ID"/></w:rPr><w:t>Penegahan</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2394" w:type="dxa"/></w:tcPr><w:p w:rsidR="00F40768" w:rsidRPr="00F40768" w:rsidRDefault="00F40768" w:rsidP="001C4173"><w:pPr><w:jc w:val="center"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/><w:lang w:val="id-ID"/></w:rPr></w:pPr><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00F40768"><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:b/><w:sz w:val="20"/><w:szCs w:val="20"/><w:lang w:val="id-ID"/></w:rPr><w:t>Explanation</w:t></w:r><w:proofErr w:type="spellEnd"/></w:p></w:tc></w:tr>';
                foreach($lineitem as $debt){
			$table .= '<w:tr>'; //new xml table row
                        
			$table .= '<w:tc><w:p><w:r><w:t>'; //start cell
			$table .= $debt->ContainerNo; //cell contents
			$table .= '</w:t></w:r></w:p></w:tc>'; //close cell
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Size;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Penegahan;
			$table .= '</w:t></w:r></w:p></w:tc>';		
			
                        $table .= '<w:tc><w:p><w:r><w:t>';
			$table .= $debt->Explanation;
			$table .= '</w:t></w:r></w:p></w:tc>';
			
                        $table .= '</w:tr>';
		} //done with dynamic data
		$table .= '</w:tbl>';
            }
            $document->setValue('LineItem',$table);
            $document->save($fileDestination);
            return true;
        }else{
            return false;    
        }
    }
}

